#include "GraphicsTool.h"
#include "ElaContentDialog.h"
#include "ElaTheme.h"
#include "ElaStatusBar.h"
#include "ElaEventBus.h"
#include "ElaCheckBox.h"
#include "ElaToolButton.h"
#include "ElaText.h"
#include "ElaToolBar.h"
#include "T_PaintWidget.h"
#include "T_TipsWidget.h"
#include "common.h"
#include <QDebug>
#include <QFileDialog>

bool g_is_tip_line = false;
QList<QString> g_item_names{};
QList<QString> g_item_group_names{};
GraphicsTool::GraphicsTool(QWidget* parent)
    : ElaWindow(parent)
{
    //主界面
    initWindow();
    //额外布局
    initEdgeLayout();
    //中心窗口
    initContent();

    //拦截默认关闭事件
    _closeDialog = new ElaContentDialog(this);
    _closeDialog->setLeftButtonText("取消");
    _closeDialog->setMiddleButtonText("最小化");
    _closeDialog->setRightButtonText("退出");
    connect(_closeDialog,&ElaContentDialog::rightButtonClicked,this,&GraphicsTool::closeWindow);
    connect(_closeDialog,&ElaContentDialog::middleButtonClicked,this,[=](){
       _closeDialog->close();
       showMinimized();
    });
    this->setIsDefaultClosed(false);
    connect(this,&GraphicsTool::closeButtonClicked,this,[=](){
        _closeDialog->exec();
    });

    //移动到中心
    moveToCenter();
}

GraphicsTool::~GraphicsTool()
{

}

void GraphicsTool::initWindow() 
{
    // setIsEnableMica(true);
    // setIsCentralStackedWidgetTransparent(true);
    setWindowIcon(QIcon(":/img/logo.png"));
    resize(800, 640);
    // ElaLog::getInstance()->initMessageLog(true);
    eTheme->setThemeMode(ElaThemeType::Light);
    // setIsNavigationBarEnable(false);
    setNavigationBarDisplayMode(ElaNavigationType::Compact);
    //setWindowButtonFlag(ElaAppBarType::MinimizeButtonHint, false);
    //setUserInfoCardVisible(false);
    setUserInfoCardPixmap(QPixmap(":/img/1.png"));
    setUserInfoCardTitle("中合聚能");
    setUserInfoCardSubTitle("evain_wl@163.com");
    setWindowTitle("PACK评估软件");
    // setIsStayTop(false);
}

void GraphicsTool::initEdgeLayout()
{
    //工具栏
    ElaToolBar* toolBar = new ElaToolBar("工具栏", this);
    toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
    toolBar->setToolBarSpacing(3);
    toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
    toolBar->setIconSize(QSize(25, 25));
    
    m_checkbox_grid = new ElaCheckBox("网格", this);
    connect(m_checkbox_grid, &ElaCheckBox::stateChanged, this, [=](int state) {
        if (state == Qt::Checked) {
            T_PaintWidgetIns->ChangeGridVisible(true);
        } else {
            T_PaintWidgetIns->ChangeGridVisible(false);
        }
    });
    m_checkbox_grid->setChecked(true);
    toolBar->addWidget(m_checkbox_grid);

    m_checkbox_line_tip = new ElaCheckBox("提示线", this);
    connect(m_checkbox_line_tip, &ElaCheckBox::stateChanged, this, [=](int state) {
        if (state == Qt::Checked) {
            g_is_tip_line = true;
        } else {
            g_is_tip_line = false;
        }        
        T_PaintWidgetIns->UpdateLineTip();
    });
    m_checkbox_line_tip->setChecked(false);
    toolBar->addWidget(m_checkbox_line_tip);

    // m_checkbox_swap_wh = new ElaCheckBox("交换长宽", this);
    // connect(m_checkbox_swap_wh, &ElaCheckBox::stateChanged, this, [=](int state) {
    //     if (state == Qt::Checked) {
    //         g_allow_swap_wh = true;
    //     } else {
    //         g_allow_swap_wh = false;
    //     }
    // });
    // m_checkbox_swap_wh->setChecked(false);
    // // toolBar->addWidget(m_checkbox_swap_wh);
    toolBar->addSeparator();

    m_tbtn_new_file = new ElaToolButton(this);
    connect(m_tbtn_new_file, &ElaToolButton::clicked, this, [=]() {
        T_PaintWidgetIns->NewFile();
    });
    m_tbtn_new_file->setText("新建模板");
    m_tbtn_new_file->setElaIcon(ElaIconType::FileCirclePlus);
    m_tbtn_new_file->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBar->addWidget(m_tbtn_new_file);

    // m_tbtn_open_file = new ElaToolButton(this);
    // connect(m_tbtn_open_file, &ElaToolButton::clicked, this, [=]() {
    //     T_PaintWidgetIns->OpenFile();
    // });
    // m_tbtn_open_file->setText("打开模板");
    // m_tbtn_open_file->setElaIcon(ElaIconType::FileImport);
    // m_tbtn_open_file->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    // toolBar->addWidget(m_tbtn_open_file);

    // m_tbtn_save_file = new ElaToolButton(this);
    // connect(m_tbtn_save_file, &ElaToolButton::clicked, this, [=]() {
    //     T_PaintWidgetIns->SaveFile();
    // });
    // m_tbtn_save_file->setText("保存模板");
    // m_tbtn_save_file->setElaIcon(ElaIconType::FileExport);
    // m_tbtn_save_file->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    // toolBar->addWidget(m_tbtn_save_file);

    // m_tbtn_export_img = new ElaToolButton(this);
    // connect(m_tbtn_export_img, &ElaToolButton::clicked, this, [=]() {
    //     T_PaintWidgetIns->ExportImage();
    // });
    // m_tbtn_export_img->setText("导出图片");
    // m_tbtn_export_img->setElaIcon(ElaIconType::FilePdf);
    // m_tbtn_export_img->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    // toolBar->addWidget(m_tbtn_export_img);

    m_tbtn_clear = new ElaToolButton(this);
    connect(m_tbtn_clear, &ElaToolButton::clicked, this, [=]() {
        T_PaintWidgetIns->ClearPaint();
    });
    m_tbtn_clear->setText("清空绘制");
    m_tbtn_clear->setElaIcon(ElaIconType::WindowRestore);
    m_tbtn_clear->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBar->addWidget(m_tbtn_clear);
    
    toolBar->addSeparator();

    this->addToolBar(Qt::TopToolBarArea, toolBar);

    //停靠窗口
    // m_dockwidget_main = new ElaDockWidget("主体信息", this);
    // m_dockwidget_main->setWidget(T_MainUiWidgetIns);
    // m_dockwidget_main->setWindowIcon(":/img/main.png");
    // T_MainUiWidgetIns->setParent(m_dockwidget_main);
    // this->addDockWidget(Qt::RightDockWidgetArea, m_dockwidget_main);
    // resizeDocks({m_dockwidget_main}, {240}, Qt::Horizontal);

    // m_dockwidget_block = new ElaDockWidget("模组信息", this);
    // m_dockwidget_block->setWidget(T_BlockUiWidgetIns);
    // m_dockwidget_block->setWindowIcon(":/img/block.png");
    // T_BlockUiWidgetIns->setParent(m_dockwidget_block);
    // this->addDockWidget(Qt::RightDockWidgetArea, m_dockwidget_block);
    // resizeDocks({m_dockwidget_block}, {240}, Qt::Horizontal);

    // m_dockwidget_sub = new ElaDockWidget("子块信息", this);
    // m_dockwidget_sub->setWidget(T_SubUiWidgetIns);
    // m_dockwidget_sub->setWindowIcon(":/img/sub.png");
    // T_SubUiWidgetIns->setParent(m_dockwidget_sub);
    // this->addDockWidget(Qt::RightDockWidgetArea, m_dockwidget_sub);
    // resizeDocks({m_dockwidget_sub}, {240}, Qt::Horizontal);
    
    //状态栏
    ElaStatusBar* statusBar = new ElaStatusBar(this);
    ElaText* statusText = new ElaText("初始化成功！", this);
    statusText->setTextPixelSize(14);
    statusBar->addWidget(statusText);
    this->setStatusBar(statusBar);
}

void GraphicsTool::initContent()
{
    addPageNode("样板绘制", T_PaintWidgetIns, ElaIconType::Paintbrush);
    addFooterNode("关于", nullptr, _aboutKey, 0, ElaIconType::User);
    T_TipsWidgetIns->hide();
    connect(this, &ElaWindow::navigationNodeClicked, this, [=](ElaNavigationType::NavigationNodeType nodeType, QString nodeKey) {
        if (_aboutKey == nodeKey)
        {
            T_TipsWidgetIns->setFixedSize(400, 400);
            T_TipsWidgetIns->moveToCenter();
            T_TipsWidgetIns->show();
        }
    });

    qDebug() << "已注册的事件列表" << ElaEventBus::getInstance()->getRegisteredEventsName();
}
